package com.masterstroke.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.masterstroke.template.common.constant.MessageConstant;
import com.masterstroke.template.common.context.BaseContext;
import com.masterstroke.template.mapper.ProductMapper;
import com.masterstroke.template.pojo.domain.Order;
import com.masterstroke.template.pojo.vo.ShowOrder;
import com.masterstroke.template.service.OrderService;
import com.masterstroke.template.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 35925
 * @description 针对表【order】的数据库操作Service实现
 * @createDate 2024-12-16 17:28:07
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
        implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ProductMapper productMapper;


    @Override
    public List<ShowOrder> getAllOrder() {
        String userId = BaseContext.getCurrentId();
        if (userId == null) {
            throw new RuntimeException(MessageConstant.USER_NOT_LOGIN);
        }
        List<Order> orders = orderMapper.getOrderByUserId(userId);
        List<ShowOrder> showOrders = new ArrayList<>();
        for (Order order : orders) {
            showOrders.add(ShowOrder.builder()
                    .price(String.valueOf(new BigDecimal(order.getProductNum()).multiply(new BigDecimal(order.getProductNum()))))
                    .orderId(order.getOrderId())
                    .productName((productMapper.selectById(order.getProductId()).getProductName()))
                    .createTime(order.getCreateTime())
                    .build());
        }
        return showOrders;
    }
}
